package com.xpqh.ksg.common.persistence.dao;

import com.xpqh.ksg.common.persistence.model.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xpqh.ksg.modular.auth.dto.KsgVerificationRespDTO;
import com.xpqh.ksg.modular.caseInfo.dto.KsgDictListRespDTO;
import com.xpqh.ksg.modular.core.dto.*;
import com.xpqh.ksg.modular.overseas.dto.KsgOverseasCarColor;
import com.xpqh.ksg.modular.overseas.dto.KsgOverseasColorOptionRespDTO;
import com.xpqh.ksg.modular.overseas.dto.KsgOverseasQuestionsRespDTO;
import com.xpqh.ksg.modular.overseas.dto.KsgStoreInfoListRespDTO;
import com.xpqh.ksg.modular.warranty.dto.CarMembrane;
import com.xpqh.ksg.modular.warranty.dto.KsgProductNameListRespDTO;
import com.xpqh.ksg.modular.warranty.dto.WarrantyInfoRespDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 案例信息表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2025-04-16
 */
public interface KsgCaseInfoMapper extends BaseMapper<KsgCaseInfo> {
    List<KsgCaseInfo> getCaseList2(@Param("vehicleBrand") String vehicleBrand,
                                 @Param("vehicleModel") String vehicleModel,
                                 @Param("productSeries") String productSeries,
                                 @Param("search") String search ,
                                 @Param("caseType") Integer caseType,
                                 @Param("classification") String classification,
                                 @Param("order") String order,
                                 @Param("label") String label,
                                 @Param("pageNo") Integer pageNo,
                                 @Param("pageSize") Integer pageSize);


    List<KsgCaseInfo> getCaseList(
            @Param("vehicleBrand") String vehicleBrand,
            @Param("vehicleModel") String vehicleModel,
            @Param("productName") String productName,
            @Param("productSeries") String productSeries,
            @Param("search") String search ,
            @Param("caseType") Integer caseType,
            @Param("kind") Integer kind,
            @Param("language") String language,
            @Param("label") String label,
            @Param("pageNo") Integer pageNo,
            @Param("pageSize") Integer pageSize);


    KsgCaseInfo getCaseDetails(@Param("id")String id);

    List<KsgDictListRespDTO> getDictList(@Param("dictType") String dictType);

    Integer getCaseListCount(@Param("vehicleBrand") String vehicleBrand,@Param("vehicleModel") String vehicleModel,@Param("productName") String productName, @Param("productSeries") String productSeries,@Param("search") String search ,@Param("caseType") Integer caseType,@Param("kind") Integer kind,@Param("language") String language);

    @Select("select user_code userId,user_name userName,extend_s2 userLevel,extend_s5 isStorage from js_sys_user where status = 0 and extend_s3 = #{account} and extend_s4 = #{password}")
    KsgVerificationRespDTO accountPassword(@Param("account") String account, @Param("password") String password);


    @Select("select id,banner,title,profiles from ksg_overseas_banner where state = 0")
    List<KsgOverseasBanner> getOverseasBannerList();

    List<KsgOverseasCaseBlog> getCaseBlogList(@Param("type")String type, @Param("pageNo") Integer pageNo, @Param("pageSize")Integer pageSize);

    Integer getCaseBlogListCount(@Param("type")String type);


    KsgOverseasCaseBlog getOverseasCaseInfoCaseInfo(String caseId);

    KsgOverseasCaseBlog getOverseasLastCaseInfoCaseInfo(@Param("caseId")String caseId,@Param("type")String type);

    KsgOverseasCaseBlog getNextOverseasCaseInfoCaseInfo(@Param("caseId")String caseId,@Param("type")String type);

    @Select("select id,car_brand carBrand,default_picture defaultPicture from ksg_overseas_color_option ")
    List<KsgOverseasColorOptionRespDTO> getOverseasColorOption();


    @Select("select car_id carId,sku,name,availability,color,color_picture colorPicture FROM ksg_overseas_car_color")
    List<KsgOverseasCarColor> getCarColorList();


    @Select("select count(1) from ksg_official_website_info where ip = #{ip} and DATE(create_time) = DATE(NOW())")
    Integer getIpCountOne(@Param("ip")String ip);

    @Select("select count(1) from ksg_overseas_contact_us where ip = #{ip} and DATE(create_time) = DATE(NOW())")
    Integer getIpCountTwo(@Param("ip")String ip);

    @Insert("insert into ksg_overseas_contact_us(describes,country,first_name,last_name,company,phone_or_whatsapp,email,questions_or_comments,interest,create_time,ip,type)" +
            "values(#{describes},#{country},#{firstName},#{lastName},#{company},#{phoneOrWhatsapp},#{email},#{questionsOrComments},#{interest},now(),#{ip},#{type})")
    Integer overseasContactUs(@Param("describes")String describes,@Param("country")String country,@Param("firstName") String firstName,@Param("lastName")String lastName,@Param("company")String company,@Param("phoneOrWhatsapp")String phoneOrWhatsapp,@Param("email") String email,@Param("questionsOrComments")String questionsOrComments,@Param("interest")String interest,@Param("ip")String ip,@Param("type")Integer type);

    @Select("select id,product_name productName,product_type productType,profiles,cover_picture coverPicture from ksg_overseas_product where state = 0 order by  is_order asc")
    List<KsgOverseasProduct> getOverseasProductList();

    @Select("select id,title,cover_picture coverPicture,subtitle,type from ksg_overseas_case_blog where state = 0 and is_home = 0 order by  is_order asc limit 0,6")
    List<KsgOverseasCaseBlog> getOverseasCaseBlogList();

    @Insert("insert into ksg_official_website_info(information,name,country,email,create_time,ip)" +
            "values(#{information},#{name},#{country},#{email},now(),#{ip})")
    Integer overseasInfoSubmit(@Param("information")String information,@Param("name")String name,@Param("country") String country,@Param("email")String email,@Param("ip")String ip);

    @Select("select product_name productName,params from ksg_overseas_product where product_type = #{productType} order by  is_order asc ")
    List<KsgOverseasProduct> getProductInfo(@Param("productType")String productType);

    @Select("select questions,answers from ksg_overseas_questios order by create_time desc")
    List<KsgOverseasQuestionsRespDTO> getOverseasQuestions();
    @Insert("insert into ksg_popup_user (phone,name,car_model,create_time,type)" +
            "values(#{phone},#{name},#{carModel},now(),#{type})")
    Integer insertPopUpUser(@Param("phone") String phone,@Param("name") String name,@Param("carModel") String carModel,@Param("type") Integer type);

    @Select("SELECT product_name productName,id productId,introduce FROM ksg_product_info")
    List<KsgProductNameListRespDTO> getProductNameListAll();
    @Select("SELECT product_name productName,id productId,introduce FROM ksg_product_info where type = 0")
    List<KsgProductNameListRespDTO> getProductNameList();
    @Select("SELECT product_series seriesName,is_select_car_model isSelectCarModel FROM `ksg_product_info` a where is_show = 0 and type = #{productType} and channel_type = #{channelType} GROUP BY product_series ORDER BY (SELECT create_time from ksg_product_series where product_series = a.product_series) asc ")
    List<KsgProductListRespDTO> getProductList(@Param("productType")Integer productType, @Param("channelType")Integer channelType);
    @Select("SELECT product_name productName,id productId,introduce,window_type windowType FROM ksg_product_info where is_show = 0 and product_series = #{productSeries} and channel_type = #{channelType} and type = #{productType}")
    List<KsgProductNameListRespDTO> getProductNameListByChannel(@Param("productSeries")String productSeries,@Param("channelType")Integer channelType,@Param("productType")Integer productType);

    @Select("SELECT product_series productSeries,id productSeriesId,introduce  FROM ksg_product_info a where id = #{productId} ")
    List<KsgProductSeriesListRespDTO> getProductSeriesList(@Param("productId") Integer productId);

    List<KsgNewsListRespDTO> searchByPc(@Param("pageNo") Integer pageNo, @Param("pageSize")Integer pageSize, @Param("search")String search);


    Integer searchByPcCount(@Param("search")String search);

    @Select("select  * from ksg_series_info where brand_id = #{brandId} order by sort asc")
    List<KsgSeriesInfo> getSeriesList(@Param("brandId")String brandId);


    List<KsgStoreInfoListRespDTO> getStoreInfoList2(@Param("pageNo") Integer pageNo, @Param("pageSize")Integer pageSize, @Param("address")String address );

    @Select("select  store_name storeName,owner_name ownerName,phone,vehicle_brand vehicleBrand,vehicle_model vehicleModel,frame_number frameNumber,email,position," +
            "construction_date constructionDate,signal_id signalId,create_time createTime,warranty_number warrantyNumber," +
            "case when (select type from ksg_product_info where id = (select product_id from ksg_stock_info where signal_id = a.signal_id)) is null then  " +
            " IFNULL((SELECT type FROM ksg_product_info WHERE product_code = TRIM(' ' FROM SUBSTRING(a.signal_id, 1,  LENGTH(SUBSTRING_INDEX(a.signal_id, '-', 1)) )) LIMIT 1 ),2) " +
            " else (select type from ksg_product_info where id = (select product_id from ksg_stock_info where signal_id = a.signal_id)) end filmType from ksg_warranty_info a where phone = #{phone} or license_plate = #{phone} or warranty_number = #{phone} or frame_number = #{phone} or email = #{phone} ORDER BY id desc limit 0,1")
    WarrantyInfoRespDTO getWarrList(@Param("phone") String phone);

    @Select("SELECT product_name productName,term from ksg_product_info where product_code = #{seriesId} limit 0,1")
    KsgProductNameListRespDTO getProductBySeriesId(@Param("seriesId") String seriesId);

    @Select("select product_model productModel,product_batch productBatch,product_box productBox,signal_id signalId from ksg_carmembrane_info where warranty_id = #{warrantyId}")
    List<CarMembrane> getCarMembraneList(@Param("warrantyId")String warrantyId);

    @Select("select  store_name storeName,owner_name ownerName,vehicle_price productPrice, phone,vehicle_brand vehicleBrand,vehicle_model vehicleModel,frame_number frameNumber,email,position," +
            "construction_date constructionDate,signal_id signalId,create_time createTime,warranty_number warrantyNumber," +
            "case when (select type from ksg_product_info where id = (select product_id from ksg_stock_info where signal_id = a.signal_id)) is null then  " +
            " IFNULL((SELECT type FROM ksg_product_info WHERE product_code = TRIM(' ' FROM SUBSTRING(a.signal_id, 1,  LENGTH(SUBSTRING_INDEX(a.signal_id, '-', 1)) )) LIMIT 1 ),2) " +
            " else (select type from ksg_product_info where id = (select product_id from ksg_stock_info where signal_id = a.signal_id)) end filmType from ksg_warranty_info a where phone = #{phone} or license_plate = #{phone} or warranty_number = #{phone} or frame_number = #{phone} or email = #{phone} ORDER BY id desc ")
    List<WarrantyInfoRespDTO> getWarrList2(@Param("phone") String phone);


    List<KsgNewsListRespDTO> getNewsList(@Param("pageNo") Integer pageNo,@Param("pageSize")Integer pageSize, @Param("searchContent") String searchContent);

    Integer getNewsListCount(String  searchContent);

    @Select("select b.user_code userCode, b.user_name operatorName, c.office_code autoId,c.address storeAddress,c.office_code storeNo,c.phone storePhone,\n" +
            "c.office_name storeName,b.extend_s5 userLevel  from ksg_users a left join js_sys_user b on a.phone = b.login_code\n" +
            "LEFT JOIN js_sys_employee emp on emp.emp_code = b.ref_code left join js_sys_office c on emp.office_code = c.office_code where a.user_id = #{userId} and b.status = '0' and emp.status = '0' and c.status = '0'")
    KsgStoreInfo getStoreInfo(@Param("userId") Integer userId);

    List<KsgDistributorBatchRespDTO> getDistributorBatch(@Param("userCode") String userCode, @Param("userLevel") Integer userLevel);

    List<KsgDistributorStoreRespDTO> getDistributorStore(@Param("userCode") String userCode, @Param("userLevel") Integer userLevel);

    List<KsgStockListRespDTO> getStockList(  @Param("type") Integer type,@Param("userCode") String userCode,@Param("userLevel") Integer userLevel,@Param("productName") String productName,@Param("productSeries") String productSeries,@Param("batchNum") String batchNum,@Param("storeId") String storeId,@Param("boxNum") String boxNum,@Param("search") String search,@Param("pageNo") Integer pageNo,@Param("pageSize") Integer pageSize);


    List< KsgStockListRespDTO> getStockListByUserCode(@Param("userCode") String userCode, @Param("searchContent")String searchContent);
}
